<script type="text/javascript">
    var resourceTree;
    $(function () {
        resourceTree = $('#resourceTree').tree({
            url: '#(ctx)/admin/roleMenu/menuList',
            parentField: 'pid',
            lines: true,
            checkbox: true,
            onClick: function (node) {
            },
            onLoadSuccess: function (node, data) {
                progressLoad();
                $.post('#(ctx)/admin/roleMenu/findMenuListByRoleId', {
                    roleId: '#(roleId)'
                }, function (result) {
                    if (result != null && result.length > 0) {
                        for (var i = 0; i < result.length; i++) {
                            if (resourceTree.tree('find', result[i].id)) {
                                resourceTree.tree('check', resourceTree.tree('find', result[i].id).target);
                            }
                        }
                    }
                }, 'json');
                progressClose();
            },
            cascadeCheck: false
        });

        $('#roleGrantForm').ajaxForm({
            dataType: "json",
            beforeSerialize: function () {
                var checknodes = resourceTree.tree('getChecked');
                var ids = [];
                if (checknodes && checknodes.length > 0) {
                    for (var i = 0; i < checknodes.length; i++) {
                        ids.push(checknodes[i].id);
                    }
                }
                $('#menuIds').val(ids);
            },
            beforeSubmit: function () {
                progressLoad();
                var isValid = $('#roleGrantForm').form('validate');
                if (!isValid) {
                    progressClose();
                }
                return isValid;
            },
            success: function (response) {
                progressClose();
                if (response.result) {
                    parent.$.modalDialog.openner_dataGrid.datagrid('reload');//之所以能在这里调用到parent.$.modalDialog.openner_dataGrid这个对象，是因为user.jsp页面预定义好了
                    parent.$.modalDialog.handler.dialog('close');
                } else {
                    parent.$.messager.alert('错误', response.msg, 'error');
                }
            }
        });
    });

    function checkAll() {
        var nodes = resourceTree.tree('getChecked', 'unchecked');
        if (nodes && nodes.length > 0) {
            for (var i = 0; i < nodes.length; i++) {
                resourceTree.tree('check', nodes[i].target);
            }
        }
    }
    function uncheckAll() {
        var nodes = resourceTree.tree('getChecked');
        if (nodes && nodes.length > 0) {
            for (var i = 0; i < nodes.length; i++) {
                resourceTree.tree('uncheck', nodes[i].target);
            }
        }
    }
    function checkInverse() {
        var unchecknodes = resourceTree.tree('getChecked', 'unchecked');
        var checknodes = resourceTree.tree('getChecked');
        if (unchecknodes && unchecknodes.length > 0) {
            for (var i = 0; i < unchecknodes.length; i++) {
                resourceTree.tree('check', unchecknodes[i].target);
            }
        }
        if (checknodes && checknodes.length > 0) {
            for (var i = 0; i < checknodes.length; i++) {
                resourceTree.tree('uncheck', checknodes[i].target);
            }
        }
    }
</script>
<div id="roleGrantLayout" class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'west'" title="系统资源" style="width: 300px; padding: 1px;">
        <div class="well well-small">
            <form id="roleGrantForm" method="post" action="#(ctx)/admin/roleMenu/grantMenu">
                <input name="roleId" type="hidden" value="#(roleId)" readonly="readonly">
                <ul id="resourceTree"></ul>
                <input id="menuIds" name="menuIds" type="hidden"/>
            </form>
        </div>
    </div>
    <div data-options="region:'center'" title="" style="overflow: hidden; padding: 10px;">
        <div>
            <button class="easyui-linkbutton" data-options="plain:true" onclick="checkAll();">全选</button>
            <br/> <br/>
            <button class="easyui-linkbutton" data-options="plain:true" onclick="checkInverse();">反选</button>
            <br/> <br/>
            <button class="easyui-linkbutton" data-options="plain:true" onclick="uncheckAll();();">取消</button>
        </div>
    </div>
</div>